<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 导入mapbox依赖 -->
    <script src='https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.js'></script>
    <link href='https://api.mapbox.com/mapbox-gl-js/v2.12.0/mapbox-gl.css' rel='stylesheet' />
    <style>
        * {
            margin: 0;
            padding: 0
        }

        #map {
            width: 100vw;
            height: 100vh;
        }
    </style>
</head>

<body>
    <!-- 2、设置地图容器的挂载点 -->
    <div id="map">

    </div>
    <script>
        /* 3、实例化地图 */
        mapboxgl.accessToken = 'pk.eyJ1IjoiY2hlbmdiZW5jaGFvIiwiYSI6ImNsODU3aGRiODA0Y2UzcHBzZmFlcmdqZ2sifQ.8k59W_pB_Riwe6o-MneRlA'
        const map = new mapboxgl.Map({
            container: "map",
            /* style-layers */
            style: "mapbox://styles/mapbox/streets-v12",
            center: [114.30, 30.50]
        })
        let isUserInterface = false;
        map.on("style.load", () => {
            map.setFog({})
        })
        /* 在地图移动完毕触发 */
        map.on("moveend", () => {
            rotateMap();
        })
        function rotateMap() {
            /* 变化经纬 */
            /* isUserInterface == false  && zoom<5 */
            let zoom = map.getZoom()
            if (!isUserInterface && zoom < 5) {
                let center = map.getCenter();
                center.lng += 5;
                map.easeTo({
                    center,
                    duration: 1000,
                    easing: n => n
                })
            }

        }
        /* 通过点击事件控制 */

        map.on("click", () => {
            isUserInterface = !isUserInterface;
            if (isUserInterface) {
                map.stop();
            } else {
                rotateMap();
            }

        })
        rotateMap();
    </script>
</body>

</html>